package com.hand.htodo.util;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.*;

public class HelloLambda {
    Runnable r1 = () -> System.out.println("Hello Lambda Expressions");
    Runnable r2 = () -> System.out.println(toString());
    @Override
    public String toString() {         return "Hello, lambda!";     }
    public static void main(String[] args) {
        new HelloLambda().r1.run();
        new HelloLambda().r2.run();
        //实现runnable接口
        new Thread( () -> System.out.println("In Java8, Lambda expression rocks !!") ).start();
        //迭代
        List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
        for (Object s:features) {
            System.out.println("传统迭代"+s);
        }
        features.forEach(n -> System.out.println(n));

        //转换
        features.forEach(System.out::println);
        //map
        Map<Integer,String> map = new HashMap<>();
        map.forEach((k,v)-> System.out.println(v));

        //排序
        /*List<Integer> str = Arrays.asList(2,1,3);
        Collections.sort(str, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        System.out.println(str);
        //lambda排序
        Collections.sort(str, Comparator.comparingInt(o -> o));
        System.out.println(str);*/

        //listener
        /*JButton button = new JButton();
        button.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                e.getItem();
            }
        });
        //lambda
        button.addItemListener(e -> e.getItem());*/
        
    }
}
